﻿using AF_Application.Interface.Cache;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Autofac;
using AF_Auth;
using AF_Application.Interface.App;
using AF_IOC;
using AF_DataModel;

namespace WebApp_Standard
{
    public partial class Authed : System.Web.UI.Page
    {

        IAppImpl App = IOC.Instance.Get<IAppImpl>();
        ISessionCacheProvider SessionCacheProvider = IOC.Instance.Get<ISessionCacheProvider>();
        protected void Page_Load(object sender, EventArgs e)
        {
            WeiXinAuthHelper WeiXinAuth = new WeiXinAuthHelper();


            WeiXinAuth.CheckAuth();
            if (WeiXinAuth.CurrentUserAuthed)
            {
                if (WeiXinAuth.CurrentUser != null)
                {
                    T_User user = App.GetUserByOpenID(WeiXinAuth.CurrentUser.OpenID);
                    if (user == null)
                    {
                        user = new T_User()
                        {
                            Id = Guid.NewGuid(),
                            OpenID = WeiXinAuth.CurrentUser.OpenID,
                            CreateTime = DateTime.Now,
                            IP = AF_Web.Distribute.GetClientIP(),
                            HeadImg = "",
                            NickName = "",
                            Score = 0,
                            PlayNum = 0,
                        };
                        App.AddUSer(user);
                    }
                    SessionCacheProvider.Set<string>("OpenId", WeiXinAuth.CurrentUser.OpenID);
                    Response.Redirect("~/Index.aspx");
                }
            }
        }


        public void WriteDebugLog(string msg)
        {
            System.IO.File.AppendAllText(Server.MapPath("~/log_" + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + ".txt"), DateTime.Now.ToString() + msg + Environment.NewLine);
        }
        public void WriteErrLog(string msg)
        {
            System.IO.File.AppendAllText(Server.MapPath("~/log_Err_" + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + ".txt"), DateTime.Now.ToString() + msg + Environment.NewLine);
        }

    }
}